package com.jy.adscm.operation.pebase.controller;


import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.operation.pebase.bean.PeAbstractContent;
import com.jy.adscm.operation.pebase.bean.PeApplyHistory;
import com.jy.adscm.operation.pebase.service.PeAbstractContentService;
import com.jy.adscm.operation.pebase.service.PeApplyHistoryService;
import com.jy.adscm.security.utils.Token;
import com.jy.adscm.support.SimpleCoreResponse;
import com.jy.adscm.utils.AdscmBaseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zyl
 * @since 2019-08-21
 */
@RestController
@RequestMapping("/adscm/pebase/peApplyHistory")
public class PeApplyHistoryController {
    @Autowired
    RedisTemplate<Object, Token> tokenRedisTemplate;
    @Autowired
    PeApplyHistoryService peApplyHistoryService;
@Autowired
    PeAbstractContentService peAbstractContentService;
    @RequestMapping("insert")
    public SimpleCoreResponse insert(@RequestBody PeApplyHistory peApplyHistory , HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        peApplyHistory.setCreator(token.getUser().getId().toString());
        peApplyHistory.setCreateTime(new Date());
        PeAbstractContent peAbstractContent = peAbstractContentService.selectById(peApplyHistory.getPid());
        if (!AdscmBaseUtils.getInstance().isNotEmpty(peAbstractContent,null)){
            throw new AdscmSystemException("peAbstractContent  该修改内容为空");
        }
        peApplyHistory.setBeforeContent(peAbstractContent.getContent());
        boolean insert = peApplyHistoryService.insert(peApplyHistory);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("delete/{id}")
    public SimpleCoreResponse delete(@PathVariable Integer  id , HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = peApplyHistoryService.deleteById(id);
        return new SimpleCoreResponse("200","新增成功",insert);
    }
    @RequestMapping("update")
    public SimpleCoreResponse update(@RequestBody PeApplyHistory  peApplyHistory , HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        boolean insert = peApplyHistoryService.updatePeApplyHistory(peApplyHistory, token);
        return new SimpleCoreResponse("200","新增成功",insert);
    }




    @RequestMapping("selectByPid/{id}")
    public SimpleCoreResponse selectByDid(@PathVariable Integer  id , HttpServletRequest request) throws AdscmSystemException, IOException {
        String tokenId = request.getParameter("tokenId");
        Token token = AdscmBaseUtils.getInstance().getTokenByTokenId(tokenId, tokenRedisTemplate);
        List<PeApplyHistory> peApplyHistorys= peApplyHistoryService.selectByDid(id);
        return new SimpleCoreResponse("200","新增成功",peApplyHistorys);
    }
}

